/*	$Id: hostection.c,v 1.3 2006/03/15 03:49:16 jfb Exp $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <canopy/log.h>
#include <canopy/string.h>

#include "msg.h"
#include "http.h"
#include "vhost.h"
#include "private.h"




/*
 * http_vhost_alloc()
 *
 * Allocate a new HTTP hostection structure and return it.
 */
http_vhost_t*
http_vhost_alloc(const char *name, const char *hostname)
{
	http_vhost_t *host;

	if ((host = malloc(sizeof(*host))) == NULL) {
		cnp_log(CNP_LOG_ERRNO, "failed to allocate virtual host");
		return (NULL);
	}
	memset(host, 0, sizeof(*host));

	host->vh_refcnt = 1;

	if ((host->vh_name = strdup(name)) == NULL) {
		cnp_log(CNP_LOG_ERRNO, "failed to allocate virtual host name");
		free(host);
	}

	if ((hostname != NULL) &&
	    ((host->vh_hostname = strdup(hostname)) == NULL)) {
		cnp_log(CNP_LOG_ERRNO, "failed to allocate virtual host name");
		free(host);
	}

	return (host);
}

/*
 * http_vhost_free()
 *
 * Free an HTTP hostection <host> and all of its associated resources.
 */
void
http_vhost_free(http_vhost_t *host)
{
	host->vh_refcnt--;

	if (host->vh_refcnt == 0) {
		if (host->vh_name != NULL)
			free(host->vh_name);

		if (host->vh_hostname != NULL)
			free(host->vh_hostname);

		free(host);
	}
}

/*
 * http_vhost_getref()
 *
 * Returns a reference to the virtual host <host>.
 */
http_vhost_t*
http_vhost_getref(http_vhost_t *host)
{
	host->vh_refcnt++;

	return (host);
}

#if 0
/*
 * http_vhost_getstate()
 *
 * Returns the hostection <host>'s state.
 */
enum http_vhost_state
http_vhost_getstate(http_vhost_t *host)
{
	return (host->vh_state);
}
#endif

/*
 * http_vhost_sethost()
 *
 *
 */
int
http_vhost_sethost(http_vhost_t *host, const char *hostname)
{
	char *copy;

	if ((copy = strdup(hostname)) == NULL) {
		cnp_log(CNP_LOG_ERRNO, "failed to copy hostname");
		return (-1);
	}
	
	if (host->vh_hostname != NULL)
		free(host->vh_hostname);

	host->vh_hostname = copy;
	cnp_log(CNP_LOG_DEBUG, "vhost %s changed hostname to %s", host->vh_name,
	    host->vh_hostname);

	return (0);
}

/*
 * http_vhost_setroot()
 *
 *
 */
int
http_vhost_setroot(http_vhost_t *host, const char *rootdir)
{
	char *copy;

	if ((copy = strdup(rootdir)) == NULL) {
		cnp_log(CNP_LOG_ERRNO, "failed to copy root directory path");
		return (-1);
	}
	
	if (host->vh_docroot != NULL)
		free(host->vh_docroot);

	host->vh_docroot = copy;
	cnp_log(CNP_LOG_DEBUG, "vhost %s changed document root to %s",
	    host->vh_name, host->vh_docroot);

	return (0);
}
